package name.raynal.tool;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

import exception.URLNotFoundException;

public class SebUrlFile extends File {

	private static final long serialVersionUID = 8274009121191076019L;
	private BufferedOutputStream outputStream;
	private String content;
	private File file;

	public SebUrlFile(String pathname) throws FileNotFoundException {
		super(pathname);
		try {
			init(new File(pathname));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public SebUrlFile(File aFile) {
		super(aFile.getParentFile(), aFile.getName());
		try {
			init(aFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void init(File aFile) throws IOException {
		file = aFile;
		file.getParentFile().mkdirs();
		file.createNewFile();
		outputStream = new BufferedOutputStream(new FileOutputStream(file));
	}

	public void copyURLcontent(String urlPath) throws URLNotFoundException {
		HttpURLConnection connexion = null;

		try {
			URL url = new URL(urlPath);
			connexion = (HttpURLConnection) url.openConnection();
			connexion.setAllowUserInteraction(true);
			DataInputStream in = new DataInputStream(connexion.getInputStream());

			if (connexion.getResponseCode() != HttpURLConnection.HTTP_OK) {
				System.err.println(connexion.getResponseMessage());
			} else {
				while (true) {
					int readByte = in.readUnsignedByte();
					outputStream.write(readByte);
				}
			}
			connexion.disconnect();
		} catch (EOFException e) {
			// Normal behavior
		} catch (FileNotFoundException fnfe) {
			throw new URLNotFoundException();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		try {
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getContent() {
		if (content == null) {
			try {
				buildContent();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return content;
	}

	private void buildContent() throws IOException {
		StringBuilder sb = new StringBuilder();
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), "UTF-8"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		String line = null;

		while ((line = reader.readLine()) != null) {
			sb.append(line + "\r\n");
		}
		content = sb.toString();
	}

	public BufferedReader getBufferedReader() {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(
						new FileInputStream(file), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return br;
	}
}
